package com.aidong.login.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;

import com.aidong.login.biz.MemberIntegralBiz;
import com.aidong.login.biz.MemberIntegralLogBiz;
import com.aidong.login.entity.MemberIntegral;
import com.aidong.login.entity.MemberIntegralLog;
import com.aidong.login.entity.MemberWithdraw;
import com.aidong.util.IntegralAction;
import com.opensymphony.xwork2.ModelDriven;


@ParentPackage("json")
@Namespace("/")
public class MemberIntegralAction implements ModelDriven<MemberIntegral> {
	private MemberIntegral memberIntegral;

	private Map<String, Object> result;

	@Override
	public MemberIntegral getModel() {
		if (memberIntegral == null) {
			memberIntegral = new MemberIntegral();
		}
		return memberIntegral;
	}

	private int page = 0;
	private int amount;
	private String name;
	private String account;

	public void setPage(int page) {
		this.page = page;
	}

	public void setAmount(int amount) {
		this.amount = amount;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	private MemberIntegralBiz integralBiz;
	private MemberIntegralLogBiz logBiz;

	@Autowired
	public void setMemberIntegralBiz(MemberIntegralBiz integralBiz) {
		this.integralBiz = integralBiz;
	}

	@Autowired
	public void setMemberInteralLogBiz(MemberIntegralLogBiz logBiz) {
		this.logBiz = logBiz;
	}

	@JSON
	public Map<String, Object> getMap() {
		return result;
	}

	@Action(value = "getIntegralLog", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String getIntegralLog() {
		result = new HashMap<String, Object>();
		List<MemberIntegralLog> logs;
		if (page == 0) {
			logs = logBiz.getLogs(memberIntegral.getIdongId());
		} else {
			logs = logBiz.getLogs(memberIntegral.getIdongId(), page);
		}
		List<Map<String, Object>> logList = new ArrayList<Map<String, Object>>();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		for (MemberIntegralLog log : logs) {
			Map<String, Object> tmp = new HashMap<String, Object>();
			tmp.put("action", IntegralAction.getAction(log.getAction()).getActionName());
			tmp.put("remark", log.getRemark());
			tmp.put("amount", log.getAmount());
			tmp.put("createTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(log.getCreateTime()));
			logList.add(tmp);
		}
		resultMap.put("log", logList);
		result.put("code", 1);
		result.put("result", resultMap);
		return "succ";
	}

	@Action(value = "getIntegral", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String integral() {
		result = new HashMap<String, Object>();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		MemberIntegral integral = integralBiz.getMemberIntegral(memberIntegral.getIdongId());
		List<MemberIntegralLog> logs = logBiz.getLogs(memberIntegral.getIdongId());
		resultMap.put("has", integral.getHas());
		resultMap.put("processing", integral.getProcessing());
		List<Map<String, Object>> logList = new ArrayList<Map<String, Object>>();
		for (MemberIntegralLog log : logs) {
			Map<String, Object> tmp = new HashMap<String, Object>();
			tmp.put("action", IntegralAction.getAction(log.getAction()).getActionName());
			tmp.put("remark", log.getRemark());
			tmp.put("amount", log.getAmount());
			tmp.put("createTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(log.getCreateTime()));
			logList.add(tmp);
		}
		resultMap.put("log", logList);
		result.put("code", 1);
		result.put("result", resultMap);
		return "succ";
	}

	@Action(value = "withdraw", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String withdraw() {
		result = new HashMap<String, Object>();
		MemberIntegral integral = integralBiz.getMemberIntegral(memberIntegral.getIdongId());
		if (amount <= 0) {
			result.put("code", 0);
			result.put("message", "提现金额不能为0");
		} else if (integral.getHas() < amount * 10) {
			result.put("code", 0);
			result.put("message", "积分不足");
		} else {
			integralBiz.withdraw(new MemberWithdraw(memberIntegral.getIdongId(), name, account, amount));
			result.put("code", 1);
		}
		return "succ";
	}

}
